var mongojs = require("mongojs");
var util = require("util");

var MongoClient = function(){
	this.init = function(){
		var url = "mongodb://127.0.0.1/eatfish";
		var collections = ["user"];
		this.db = mongojs(url, collections);
	},
	this.insert = function(data, cb){
		this.db.user.insert(data, function(err, data){
			console.log("ins data ", data);
			if(err)
				cb(false);
			else
				cb(true);
		});
	},
	this.update = function(cond, data, cb){
		this.db.user.update(cond, data, {upsert:true}, function(err, data){
			console.log("up data", data);
			if(err)
				cb(false);
			else
				cb(true);
		});
	},
	this.remove = function(cond, cb){
		this.db.user.remove(cond, function(err, data){
			console.log("rem data", data)
			if(err)
				cb(false);
			else
				cb(true);
		});
	},
	this.find = function(cond, cb){
		this.db.user.find(cond, function(err,res){
			console.log("find res", res);
			if(err)
				cb(false);
			else
				cb(res);
		});
	},
	this.close = function(){
		this.db.close();
	}
}

module.exports = new MongoClient();
/*
MongoClient.prototype.update = function(cond, data, cb){
	var db = new MongoClient();
	db.user.update(cond, data, {upsert:true}, function(err, data){
		db.close();
		console.log("up data", data);
		if(err)
			cb(false);
		else
			cb(true);
	});
}

MongoClient.prototype.remove = function(cond, cb){
	var db = new MongoClient();
	db.user.remove(cond, function(err, data){
		db.close();
		console.log("rem data", data)
		if(err)
			cb(false);
		else
			cb(true);
	});
}

MongoClient.prototype.find = function(cond, cb){
	var db = new MongoClient();
	db.user.find(cond, function(err,res){
		db.close();
		console.log("find res", res);
		if(err)
			cb(false);
		else
			cb(res);
	});
}


var cond = {
	uid: 2
};

MongoClient.find(cond, function(data){
	console.log("find data", data);
});

var data = {
	uid: 2,
	level: 1,
	lastPlayTime: Date.now()
};
MongoClient.update(cond, data, function(data){
	console.log("update data", data);
});
*/


